﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific folder governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class UserMessageFolderController : DefaultController
	{
		public UserMessageFolderController(IUserMessageFolderService userMessageFolderService)
		{
			this.UserMessageFolderService = userMessageFolderService;
		}

		public IUserMessageFolderService UserMessageFolderService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index()
		{
			var folders = UserMessageFolderService.GetAll();
			var folder = folders.FirstOrDefault();

			return folder.CanView ? View(Views.Index, folders) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var folder = UserMessageFolderService.Create();

			return folder.CanCreate ? View(Views.Create, new UserMessageFolderCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(UserMessageFolderCreate value)
		{
			try
			{
				var folder = UserMessageFolderService.Create();

				if (!folder.CanCreate)
				{
					return NotAuthorized();
				}

				this.UserMessageFolderService.Insert(folder, value);

				return Message(Routes.UserMessageFolderUpdate, new { id = folder.Id }, Resources.Messages.UserMessageFolderCreated.Fmt(folder.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var folder = UserMessageFolderService.GetById(id);

			if (folder == null)
			{
				return Error(Resources.Messages.UserMessageFolderNotFound);
			}

			return folder.CanUpdate ? View(Views.Update, new UserMessageFolderUpdate(folder)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(UserMessageFolderUpdate value)
		{
			try
			{
				var folder = UserMessageFolderService.GetById(value.Id);

				if (folder == null)
				{
					return Error(Resources.Messages.UserMessageFolderNotFound);
				}

				if (!folder.CanUpdate)
				{
					return NotAuthorized();
				}

				this.UserMessageFolderService.Update(folder, value);

				return Message(Routes.UserMessageFolderUpdate, new { id = folder.Id }, Resources.Messages.UserMessageFolderUpdated.Fmt(folder.Title));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var folder = this.UserMessageFolderService.GetById(id);

			if (folder == null)
			{
				return base.Error(Resources.Messages.UserMessageFolderNotFound);
			}

			return folder.CanDelete ? base.View(Views.Delete, new UserMessageFolderDelete(folder)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(UserMessageFolderDelete value)
		{
			try
			{
				var folder = this.UserMessageFolderService.GetById(value.Id);

				if (folder == null)
				{
					return base.Error(Resources.Messages.UserMessageFolderNotFound);
				}

				if (!folder.CanDelete)
				{
					return NotAuthorized();
				}

				this.UserMessageFolderService.Delete(folder);

				return base.RedirectToRoute(Routes.UserMessageFolderIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, value);
		}
	}
}